import Vue from "vue";
import App from "./App.vue";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import "@/css/style.css";

import router from "./router";

import axios from "axios";

axios.defaults.baseURL = import.meta.env.VITE_SERVE_BASE_URL;

axios.interceptors.request.use(function (config) {
  config.headers.authorization = localStorage.getItem("authorization");

  return config;
});

axios.interceptors.response.use(function (response) {
  var data = response.data;

  if (data.code === 401) {
    localStorage.removeItem("authorization");
    localStorage.removeItem("user");
    router.replace("/login");
    return Promise.reject(data.message);
  } else if (data.code !== 1) {
    return Promise.reject(data.message);
  }

  return data;
});

Vue.use(ElementUI);
new Vue({
  router,
  render: (h) => h(App),
}).$mount("#app");
